package com.quanquan.management.controller.marketActivity;

import DTO.PageDTO;
import base.BaseConst;
import base.BaseController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.*;
import com.quanquan.management.request.marketActivity.ScratchCardRequest;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import request.PageRequestBody;
import utils.Request;
import utils.StringUtils;
import web.response.ResultBody;

import java.util.List;

/**
 * @ClassName ScratchCardController
 * @Description
 * @Author Owen
 * @Date 2019/8/28 0028 下午 3:18
 * @Version V1.0
 **/
@Controller
@RequestMapping("/marketActivity/scratchCard")
public class ScratchCardController extends BaseController {

    @Value("${module-url.storeUrl}")
    private String storeUrl;

    @RequestMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate,
                       Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        PageRequestBody requestBody = new PageRequestBody(page, pageSize, keyword, null, false);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);

        String json = Request.postJson(storeUrl + "marketActivity/scratchCard/getAllScratchCardList", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        PageDTO<ScratchCardDTO> pageDTO = null;
        if (0 == resCode) {
            pageDTO = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
        }

        model.addAttribute("pageObj", pageDTO);
        return "marketActivity/scratchCard/list";
    }

    @RequestMapping("/add")
    public String add(String id,ScratchCardRequest requestBody,Model model) {
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            if(StringUtils.isNotEmpty(id)){
                requestBody.setId(Long.parseLong(id));
                String json = Request.postJson(storeUrl + "marketActivity/scratchCard/getInfoById", requestBody);
                JSONObject jsonObject = JSONObject.parseObject(json);
                System.out.println(jsonObject.toJSONString());
                Integer resCode = jsonObject.getInteger("resCode");
                if (resCode == 0) {
                    ScratchCardDTO scratchCardDTO = jsonObject.getJSONObject("data").toJavaObject(ScratchCardDTO.class);
                    model.addAttribute("editable",scratchCardDTO);
                }
            }
            return "marketActivity/scratchCard/form";
        }
        String json = Request.postJson(storeUrl + "marketActivity/scratchCard/edit", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (resCode == 0) {
            return "redirect:/marketActivity/scratchCard/list";
        }
        return null;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResultBody delete(String id,ScratchCardRequest requestBody) {
        requestBody.setId(Long.parseLong(id));
        requestBody.setStatus(2);
        String json = Request.postJson(storeUrl + "marketActivity/scratchCard/edit", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (resCode == 0) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),"success");
        }
        return new ResultBody(ResCodeEnum.CUSTOM.getCode(),"删除失败");
    }

    @RequestMapping("/gift")
    public String gift(@RequestParam(required = false) Long id,
                       @RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate,
                       Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        System.out.println(page);
        ScratchCardRequest requestBody = new ScratchCardRequest(page, pageSize, keyword, null, false);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setId(id);
        String json = Request.postJson(storeUrl + "marketActivity/scratchCard/getScratchCardGifts", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        PageDTO<ScratchCardGiftDTO> pageDTO = null;
        if (0 == resCode) {
            pageDTO = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            System.out.println(JSON.toJSONString(pageDTO));
        }

        model.addAttribute("pageObj", pageDTO);
        model.addAttribute("scratchCardId",id);
        return "marketActivity/scratchCard/gift";
    }


    @RequestMapping("/addGift")
    public String addGift(String id,String scratchCardId,ScratchCardRequest requestBody,Model model) {
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            String json = Request.postJson(storeUrl + "coupon/query_coupon_list", requestBody);
            JSONObject jsonObject = JSONObject.parseObject(json);
            Integer resCode = jsonObject.getInteger("resCode");
            if (resCode == 0) {
                List<CouponDTO> couponDTOs = jsonObject.getJSONArray("data").toJavaList(CouponDTO.class);
                model.addAttribute("couponList",couponDTOs);
            }
            if(StringUtils.isNotEmpty(id)){
                requestBody.setId(Long.parseLong(id));
                json = Request.postJson(storeUrl + "marketActivity/scratchCard/getGiftInfoById", requestBody);
                jsonObject = JSONObject.parseObject(json);
                resCode = jsonObject.getInteger("resCode");
                if (resCode == 0) {
                    ScratchCardGiftDTO scratchCardGiftDTO = jsonObject.getJSONObject("data").toJavaObject(ScratchCardGiftDTO.class);
                    model.addAttribute("editable",scratchCardGiftDTO);
                }
            }

            model.addAttribute("scratchCardId",scratchCardId);
            return "marketActivity/scratchCard/addGift";
        }
        requestBody.setGiftType(1);
        requestBody.setPoints(0);
        System.out.println(JSON.toJSONString(requestBody));
        String json = Request.postJson(storeUrl + "marketActivity/scratchCard/editGift", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (resCode == 0) {
            return "redirect:/marketActivity/scratchCard/gift?id="+scratchCardId;
        }
        return null;
    }

    @RequestMapping("/deleteGift")
    @ResponseBody
    public ResultBody deleteGift(String id,ScratchCardRequest requestBody) {
        requestBody.setId(Long.parseLong(id));
        requestBody.setStatus(2);
        String json = Request.postJson(storeUrl + "marketActivity/scratchCard/editGift", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (resCode == 0) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),"success");
        }
        return new ResultBody(ResCodeEnum.CUSTOM.getCode(),"删除失败");
    }

    @RequestMapping("/record")
    public String record(@RequestParam(required = false) Long id,
                       @RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate,
                       Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        ScratchCardRequest requestBody = new ScratchCardRequest(page, pageSize, keyword, null, false);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setId(id);
        String json = Request.postJson(storeUrl + "marketActivity/scratchCard/getScratchCardRecords", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        PageDTO<ScratchCardRecordDTO> pageDTO = null;
        if (0 == resCode) {
            pageDTO = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
        }

        model.addAttribute("pageObj", pageDTO);
        model.addAttribute("scratchCardId",id);
        return "marketActivity/scratchCard/record";
    }
}
